Creating Your First Macromedia Central Application
Table of Contents
Creating an Agent
Your Central product can include several applications. (You might call the product a "suite" in that case.) For each application you can have multiple pods; in fact, after users install the application you can add the pods dynamically—without their intervention. Besides pods, your application can include up to one agent.
An agent is a SWF that's never seen. However, you can set your agent to start every time that Central does. This means code continues executing even when your application is closed and Central is minimized. Agents are useful for performing background tasks such as checking current weather conditions. When the temperature drops, for example, you could inform the user through a notice that behaves almost like an instant message. Not only are agents good for behind-the-scenes work but they're the logical place to store code that's common to your application and pods. This way it's available anytime the application needs it.
Sometimes commands are only available to pods, such as loadApplication().
Other times only applications can trigger them, such as requestPayment().
Generally, the agent—and its "shell" the Agent Manager—is the common
denominator: It runs constantly and most commands are available there.
The ways that your application, pods, and agent talk to Central is
through their respective managing objects: Shell, Console, and Agent
Manager. You can see in Table 1 that most of the methods are available
for all three objects. That is, you can put code where you want. However,
some methods don't apply to certain situations. For example, a pod
doesn't need to use isConsoleOpen() to find out whether
the Console is open because the Console must be open in the first place
for the pod to find out. There are other logical restrictions where
one object doesn't support a particular method, but methods are available
where you need them. For the most part it's up to you whether you trigger
a method from your application, agent, or pod(s).
| Methods | Agent Manager | Shell | Pod |
|---|---|---|---|
| Notices | c | ||
| addNotice | ![]() |
![]() |
![]() |
| getNotices | ![]() |
![]() |
![]() |
| removeNotice | ![]() |
![]() |
![]() |
| Local Internet Cache | |||
| addToLocalInternetCache | ![]() |
![]() |
![]() |
| inLocalInternetCache | ![]() |
![]() |
![]() |
| removeFromInternetCache | ![]() |
![]() |
![]() |
| Information | |||
| isConsoleOpen | ![]() |
![]() |
![]() |
| isConnected | ![]() |
![]() |
![]() |
| getPreferences | ![]() |
![]() |
![]() |
| getHeight | ![]() |
![]() |
![]() |
| getLastTabIndex | ![]() |
![]() |
![]() |
| Pod Management | |||
| getPods | ![]() |
![]() |
![]() |
| addPod | ![]() |
![]() |
![]() |
| viewPod | ![]() |
![]() |
![]() |
| removePod | ![]() |
![]() |
![]() |
| getViewedPods | ![]() |
![]() |
![]() |
| Application Management | |||
| getViewedApplications | ![]() |
![]() |
![]() |
| loadApplication | ![]() |
![]() |
![]() |
| Agent Management | |||
| startAgent | ![]() |
![]() |
![]() |
| stopAgent | ![]() |
![]() |
![]() |
| getAgent | ![]() |
![]() |
![]() |
| Miscellaneous | |||
| editLocationDialog | ![]() |
![]() |
![]() |
| newLocationDialog | ![]() |
![]() |
![]() |
| requestSizeChange | ![]() |
![]() |
![]() |
| validateActivationKey | ![]() |
![]() |
![]() |
| requestPayment | ![]() |
![]() |
![]() |
| getBounds | ![]() |
![]() |
![]() |
| getSelectedItem | ![]() |
![]() |
![]() |
| setProgress | ![]() |
![]() |
![]() |
| setStatus | ![]() |
![]() |
![]() |
| isPurchased | ![]() |
![]() |
![]() |
While it's impossible to put all your code in the agent, you should definitely try to keep most common elements here. Ultimately, you need to send messages among your agent, pod, and application. The channel over which the pieces communicate is the Local Connection object. You can use the existing version in Macromedia Flash, but Macromedia Central has a much more eloquent version called LCService. This class not only simplifies managing multiple connections (say, many pods and application instances) but, because it's synchronous, you can set up methods that return values. I use it next to let your application and pods trigger code in the agent, as well as let the agent trigger code in the application and pods.
Note: Just in case you're thinking of using local shared objects to make the different pieces of your product communicate with each other, don't. You'll run into problems if two files attempt to read and write at the same time. Besides, the LCService class makes this task very easy. For more information about LCService, read Ethan Malasky's article, "Understanding and Using the LCService Library."
In the next exercise I show you that adding an agent is nearly the
same as making the application or pod. The key is to select the appropriate
tasks for your agent to perform. Generally speaking, this includes
tasks you want to execute independently of whether the user is actively
running your application or pod. In your agent, for example, you could
use setInterval() to trigger a function that accesses
a web service every 10 minutes and finds out the current temperature
in the user's city. Of course, the user would need to use either a
pod or application as an interface to set their city. Similarly, if
the agent reports the results it gathers it needs to send that information
to a pod, application, or—perhaps—a notice. Agents don't start and
stop as your application is opened and closed. Therefore, carefully
deciding what tasks your agent executes (anytime Central is open) is
both an opportunity and a responsibility.
The basic idea behind LCService is that you want "clients" (the pods
and application) to access methods in the "server" (the agent). Conversely,
the agent should have access to methods in the pods and application.
Because it's all handled synchronously, the pods, application, and
agent can trigger—and get values returned—from any method in your product.
You just need to specify which methods to expose to the LCService.
The following steps show you how to define the methods in a file called interface.as:
-
Create a new ActionScript file in Flash MX Professional 2004 or in a text editor. Save the file as interface.as. Insert the following code:
myInterface=new Object(); myInterface.name="myInterfaceName"; myInterface.interfaces=new Object(); myInterface.interfaces.Client=["tellEveryone"]; myInterface.interfaces.Server=["agentMethod", "forwardMethod"];
Here's the simple way to read this code: We're planning to have one method in the application and pods (
tellEveryone) and two methods in the agent (agentMethodandforwardMethod). The LCService class uses a client/server metaphor where the agent is the server and the pods and application are the client. Each file in your product needs the same interface definition—which will be easy to include now that you have a separate file (interface.as). -
Create a new file, as you did in the earlier exercises. This time don't bother with anything on the Stage because the agent is never visible. Just set the publish settings to Flash Player 6 and uncheck the HTML format.
Also, put this code in the first frame:
#include "interface.as"
#include "agent.as" - Save this file as agent.fla.
- Create a new ActionScript file and save it as agent.as, adjacent to agent.fla.
-
Type the following code:
1 owner=this; 2 agentMethod=function(param){ 3 return "I heard "+param; 4 } 5 forwardMethod=function(param){ 6 myService.tellEveryone(param); 7 } 8 onActivate = function(agentManager, agentID, data) { 9 gShell=agentManager; 10 myService=Central.LCService.createServer(myInterface, owner, true); 11} 12 Central.initAgent (this, this);The
onActivate()andinitAgent()methods should look familiar. The main thing inonActivate()is creating a server instance of the LCService class. ThemyServicevariable is the way the agent triggers code in the pod or application (as on line 6). ThecreateServer()method specifies themyInterfacevariable (defined in the interface.as file) and passes a reference to the place where server methods will be found (this timeline) and opt for the synchronous behavior (in the true parameter).Also, recall from interface.as that the two methods (
agentMethodandforwardMethod) are exposed to the clients—that is, the pod and application. Just as how line 6 accesses thetellEveryone()method (to be defined in the application and pod) using a reference to the LCService, the pod and application similarly accessagentMethod()andforwardMethod(). - Save agent.as, return to agent.fla, and publish agent.swf (press Shift-F12).
-
Open product.xml in order to add the pod and increment the version number. Specifically, change this line:
version="1.1"so it reads:
version="1.2"Also change this line:
<!--<agent name="Sample Agent" src="agent.swf" started="true"/>-->so it reads:
<agent name="Sample Agent" src="agent.swf" started="true"/> - Save product.xml.
Notice that the started attribute means that this agent launches as
soon as Central does. (Otherwise you have to start it with the startAgent() command.)
Before you actually update the application, first update the pod and application files so that they use the agent:
-
Open pod.as and replace all the code with the following:
owner=this; tellEveryone=function(param){ ta.text+=param+newline; } onActivate = function(console, podID, viewerID, position, baseTabIndex, data) { gShell=console; var uniqueID = podID+"_"+viewerID; myService=Central.LCService.createClient(myInterface, uniqueID, owner, true); } Central.initPod(this, this);In this case we're creating a client version of the LCService class, saved in the
myServicevariable. Like thecreateServer()method,createClient()needs the variable containing the interface (myInterface—which gets defined ininterface.as) and object where the methods will reside (owner, that is this timeline) and whether you want it to behave synchronously (true). The second parameter sent (uniqueID), however, is a unique string to prevent clients from conflicting. It’s not needed withcreateServer()because there can be only one "server."The
tellEveryone()function is simply the pod's implementation of this method. That is, the application has the same method but it executes different code. Finally, although the application triggers the agent'sforwardMethod(), coming up in Step 4 below, nowhere does the pod itself access either of the two agent's methods. -
Save pod.as and open pod.fla. Edit the script to read as follows:
#include "interface.as"
#include "pod.as" - Export it to create pod.swf.
-
Open app.as and change the code to read as follows:
owner=this; tellEveryone=function(param){ ta.text+=param+newline; gShell.setStatus(param); } echo_btn.onPress=function(){ ta.text+=myService.agentMethod(input_txt.text); } send_btn.onPress=function(){ myService.forwardMethod(input_txt.text); } onActivate = function(shell, appID, shellID, baseTabIndex, data) { gShell=shell; var uniqueID = appID+"_"+shellID; myService=Central.LCService.createClient(myInterface, uniqueID, owner, true); } Central.initApplication(this, this);This is just a bit more than the pod's code. Notice that the implementation of
tellEveryoneis slightly different than the pod's version. (Here you change the text in the shell's status bar.) There are also two button instances in the movie (echo_btnandsend_btn) plus a text field instance (input_txt). The echo button just proves that you can call a method in the agent and get a return value. The send button is a bit simpler: It just passes data to the agent, although the agent turns around and sends that data back through thetellEveryonemethod. (In the last part of this tutorial you modify the agent'sforwardMethodso that it looks up the current temperature for a ZIP code that you send it using this button.) -
Return to app.fla and modify the code to read as follows:
#include "interface.as"
#include "app.as" - Before you publish, place two buttons on the Stage (instance names echo_btn and send_btn) and one input text field (named input_txt). Then publish it to create app.swf.
-
Test it out! Launch the application, check for updates, and install the new version. Be sure the pod is open and try typing into the input text field and clicking either button. The
send_btnpasses the string to the agent, which then sends it back to both the pod and application (with the application also changing the status text area). Theecho_btnsimply gets a value back from the agent and places it into the TextArea component.Note: If you have problems with this portion, the completed files are in the folder "part3_finished--Creating an Agent.".

